package com.taru.houseapp.controller;

import com.taru.houseapp.commen.JsonResult;
import com.taru.houseapp.entity.SearchRecord;
import com.taru.houseapp.entity.User;
import com.taru.houseapp.entity.VoPassword;
import com.taru.houseapp.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 判断用户是否被注册过
     * @param userName
     * @return
     */

    @GetMapping ("/user/exist")
    public JsonResult userIsExist(String userName){
        JsonResult result=null;
        if(userName==null || userName.length()<=0){
            result=new JsonResult(404,"参数错误");
            return result;
        }
        try{
            boolean isExist=userService.queryUserIsExist(userName);
            result=new JsonResult(200,"获取成功",isExist);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 用户注册
     * @param user
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public JsonResult addUser(@RequestBody  User user){
        JsonResult result=null;
        try {
            int code=userService.addUser(user);
            if(code!=0){
                result=new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 根据用户id查询用户详情
     * @param userId
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public JsonResult queryUserById(String userId){
        JsonResult result=null;
        try{
            User user=userService.queryUserById(userId);
            if(user!=null){
                result=new JsonResult(200,"获取成功",user);
            }else {
                result=new JsonResult(404,"参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }
    /**
     * 修改用户信息
     * @param user
     */
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public JsonResult updateUserById(@RequestBody User user){
        JsonResult result=null;
        try{
            int code=userService.updateUserById(user);
            if(code==1){
                result=new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 修改密码
     * @param voPassword
     */
    @RequestMapping(value = "/user/password",method = RequestMethod.PUT)
    public JsonResult updatePasssword(@RequestBody VoPassword voPassword){
        JsonResult result=null;
        try{
            int code=userService.updatePasssword(voPassword);
            if(code==1){
                result=new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"原密码错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 修改头像
     * @param user
     */
    @RequestMapping(value = "/user/header",method = RequestMethod.PUT)
    public JsonResult updateHeaderById(@RequestBody User user){
        JsonResult result=null;
        try{
            int code=userService.updateHeaderById(user);
            if(code==1){
                result=new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return  result;
    }

    /**
     * 注销账号
     * @param user
     */
    @RequestMapping(value = "/user/logout",method = RequestMethod.DELETE)
    public JsonResult deleteUserById(@RequestBody User user){
        JsonResult result=null;
        try{
            int code=userService.deleteUserById(user);
            if(code==1){
                result=new JsonResult(200,"注销成功");
            }else {
                result=new JsonResult(404,"参数错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 忘记密码
     * @param user
     * @return
     */
    @RequestMapping(value = "/user/password/replace",method = RequestMethod.PUT)
    public JsonResult updatePasswordByUserName(@RequestBody User user){
        JsonResult result=null;
        try{
            int code=userService.updatePasswordByUserName(user);
            if(code>0){
                result=new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 查询历史搜索记录
     * @return
     */
    @RequestMapping(value = "/record/{id}",method = RequestMethod.GET)
    public JsonResult querySearchRecord(@PathVariable("id") String id){
        JsonResult result=null;
        if(id ==null || id.length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            List<SearchRecord> records=userService.querySearchRecord(id);
            result=new JsonResult(200,"获取成功",records);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 添加历史搜索记录
     * @param record
     * @return
     */
    @RequestMapping(value = "/record",method = RequestMethod.POST)
    public JsonResult insertSearchRecord(@RequestBody SearchRecord record){
        JsonResult result=null;
        if(record ==null || record.getContext()==null || record.getContext().length()<=0||record.getUserId()==null || record.getUserId().length()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            userService.insertSearchRecord(record);
            result=new JsonResult(200,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 清空历史记录
     * @param ids
     * @return
     */
    @RequestMapping(value = "/record",method = RequestMethod.DELETE)
    public JsonResult deleteSearchRecord(@RequestBody List<Integer> ids){
        JsonResult result=null;
        if(ids==null || ids.size()<=0){
            result=new JsonResult(404,"参数错误");
            return  result;
        }
        try{
            userService.deleteSearchRecord(ids);
            result=new JsonResult(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }
}
